package org.jeecg.chase.common.engine.service;

import org.jeecg.chase.common.supper.process.CommonProcess;
import org.jeecg.chase.common.utils.ConditionExpressionUtil;
import org.jeecg.chase.common.utils.InvokeSupportUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author leikai
 * @version 1.0
 * @description:数据处理
 * @date 2021/07/27/0027 14:26
 */
@Service
public class ProcessService {

    public void process(Object object, List<CommonProcess> processList) {
        for (CommonProcess process : processList) {
            this.process(object, process);
        }
    }

    public Object process(Object object, CommonProcess commonProcess) {
        Class<?>[] paramTypes = commonProcess.getParamTypes();
        if (paramTypes.length > 0) {
            Object processedObject = object;
            Object precessService = commonProcess.getServiceObject();
            Method method = commonProcess.getMethodObject();
            List<Object> paramList = new ArrayList<Object>();
            paramList.addAll(commonProcess.getParamList());

            //脚本的第一个参数（被处理对象）类型是否为集合型
            Boolean firstParamTypeIsIterable = commonProcess.getFirstParamTypeIsIterable();

            //当前被处理对象的类型是否为集合型
            Boolean processedObjectTypeIsIterable = Iterable.class.isAssignableFrom(object.getClass());

            //当前对象是否需要被处理
            Boolean needProcesse = true;

            if (true == processedObjectTypeIsIterable) {
                //当前被处理的对象为集合型
                if (true == firstParamTypeIsIterable) {
                    //脚本中承接被处理对象的参数类型为集合型
                    //此时不判断条件表达式，此时的判断逻辑在脚本中实现
                    return invoke(precessService, method, processedObject, paramList);
                } else {
                    //脚本中承接处理对象的参数类型非集合型
                    List list = (List) processedObject;
                    for (Object obj : list) {
                        needProcesse = ConditionExpressionUtil.needExecute(obj, commonProcess);
                        if (true == needProcesse) {
                            processedObject = obj;

                            paramList.clear();
                            paramList.addAll(commonProcess.getParamList());

                            invoke(precessService, method, processedObject, paramList);
                        }
                    }
                }
            } else {
                //当前被处理的对象非集合型
                needProcesse = ConditionExpressionUtil.needExecute(object, commonProcess);
                if (false == needProcesse) {
                    return null;
                }
                if (true == firstParamTypeIsIterable) {
                    //脚本中承接被处理对象的参数类型为集合型,需将当前对象放入list再被处理
                    ArrayList<Object> objList = new ArrayList<Object>();
                    objList.add(object);
                    processedObject = objList;
                } else {
                    //脚本中承接被处理对象的参数类型不是集合型
                    processedObject = object;
                }
                return invoke(precessService, method, processedObject, paramList);
            }
        }
        return null;
    }

    private Object invoke(Object precessService, Method method, Object processedObject, List<Object> paramList) {
        paramList.add(0, processedObject);
        Object[] paramArray = paramList.toArray();
        return InvokeSupportUtils.invokMethod(method, precessService, paramArray);
    }

}
